const URI = 'https://steam.app.gojnr.com'
const fetch = require('./fetch')

/**
 * 抓取豆瓣电影特定类型的API
 * https://developers.douban.com/wiki/?title=movie_v2
 * @param  {String} type   类型，例如：'coming_soon'
 * @param  {Objece} params 参数
 * @return {Promise}       包含抓取任务的Promise
 */
function fetchApi (type, params) {
  return fetch(URI, type, params)
}

/**
 * 获取首页卡片
 * @param  {Number} page   页码
 * @param  {Number} count  页条数
 * @return {Promise}       包含抓取任务的Promise
 */
function getCard(){
    fetch(URI,"",{
        "":"",
        "":""
    }).then(res => res.data)    
} 


function getCardDetail  (type,params){
    return fetch(URI,type,params)
}

function chooseCard(type,params){
    return fetch(URI,type,params)
}

/** 
 *   发送评论
 *   param      {} <who> { 发送者 }
 *   param      {} <to> { 接受者 }
 *   param      {} <text> { 发送文本 }
 */
function sendReplyTo(who,to){
    //回复
    fetch(URI,"",{
        "":"",
        "":""
    }).then(res => res.data)
}

/**
 * 登录授权
 */
function login(){
    fetch(URI,"",{
        "":"",
        "":""
    }).then(res => res.data)    
}


/**
 * { function_description }
 *
 * @param      {<type>}  res     The resource
 * @return     {Array}   { description_of_the_return_value }
 */
function dealWithData(res){
      var imgArr = [];
      var Collection =  [];
      console.log(res.data)
      res.data.forEach(function(v,i){

      if(v.played!= "" ){
        v.played.forEach(function(t,h){
            imgArr.push(t.logo);
        })
      }

         var tempObj = {
          "steamName":v.st_nickname,
          "user_id":v.user_id,
          "steamId":v.steam_id,
          "steamPrice":v.account_price,
          "steamTime":v.play_time,
          "steamGames":v.game_owned,
          "steamIntroduce":v.saying,
          "steamGamesShow":imgArr,
          "steamPicture":v.st_avatar,
          "aim_id":v.user_id,
          animationData:{}
        }
        imgArr = [];
        Collection.push(tempObj);

      })
      return Collection;
  }
  /**
   *  转换时间
   *
   * @param      {<type>}           time    The time
   * @return     {(number|string)}  { description_of_the_return_value }
   */
    function convertime(time){
        //JavaScript函数：
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        function Appendzero(obj){
             if(obj<10) return "0" +""+ obj;
                 else return obj;
        }
        function getDateDiff(dateTimeStamp){
            var now = new Date().getTime();
            console.log(now)
            console.log(dateTimeStamp)
            var dateTimeStamp = new Date(dateTimeStamp * 1000).toLocaleString(); 
            var diffValue = now - dateTimeStamp;

            console.log(diffValue)
            if(diffValue < 0){
             //若日期不符则弹出窗口告之
             //alert("结束日期不能小于开始日期！");
             }
            var monthC =diffValue/month;
            var weekC =diffValue/(7*day);
            var dayC =diffValue/day;
            var hourC =diffValue/hour;
            var minC =diffValue/minute;
            if(monthC>=1){
             result="发表于" + parseInt(monthC) + "个月前";
             }
             else if(weekC>=1){
             result="发表于" + parseInt(weekC) + "周前";
             }
             else if(dayC>=1){
             result="发表于"+ parseInt(dayC) +"天前";
             }
             else if(hourC>=1){
             result="发表于"+ parseInt(hourC) +"个小时前";
             }
             else if(minC>=1){
             result="发表于"+ parseInt(minC) +"分钟前";
             }else
             result="刚刚发表";

             console.log(result)
            return result;
        }
        return getDateDiff(time);
    }



/**
 * 获取单条类型的数据
 * @param  {Number} id     电影ID
 * @return {Promise}       包含抓取任务的Promise
 */
// function findOne (id) {
//   return fetchApi('subject/' + id)
//     .then(res => res.data)
// }

module.exports = { login, sendReplyTo ,getCard,getCardDetail ,chooseCard,dealWithData,convertime}
